package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GouMaiKeChengActivity;
import com.xgkj.diyiketang.activity.ZhiboPasswordActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerActivity;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沐诺 on 2017/4/9.
 */

public class VideoLiveLAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<>();

    public VideoLiveLAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void updateData(List<EatShowUserInfo> list, boolean isclear) {
        if (mList != null && isclear) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            viewHolder = new VideoLiveLAdapter.ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.livelist_item, null);
            viewHolder.anchorImgView = (ImageView) convertView.findViewById(R.id.head_img);
            viewHolder.fansNum = (TextView) convertView.findViewById(R.id.renshu);
            viewHolder.livegift = (TextView) convertView.findViewById(R.id.item_livelist_gift);
            viewHolder.liveman = (TextView) convertView.findViewById(R.id.item_livelist_man);
            viewHolder.anchorNameText = (TextView) convertView.findViewById(R.id.nickname);
            viewHolder.fengmianImg = (ImageView) convertView.findViewById(R.id.fengmian);
            viewHolder.liveTitle = (TextView) convertView.findViewById(R.id.live_title);
            viewHolder.liveState = (TextView) convertView.findViewById(R.id.state);
            viewHolder.charge_or_pwdText = (ImageButton) convertView.findViewById(R.id.charge_or_pwd);
            viewHolder.zhiboTimeText = (TextView) convertView.findViewById(R.id.zhibo_time);
            viewHolder.liveDesc = (TextView) convertView.findViewById(R.id.live_desc);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        EatShowUserInfo info = mList.get(position);
        if (info != null) {
            String anchorImgUrl = info.getUserLogo();
            if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(anchorImgUrl, size));
                if (cacheBitmap != null) {
                    viewHolder.anchorImgView.setImageBitmap(cacheBitmap);
                } else {
                    viewHolder.anchorImgView.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.anchorImgView, anchorImgUrl, size);
                }
            } else {
                viewHolder.anchorImgView.setImageResource(R.drawable.default_head_image);
            }
            if (info.getNickName() != null) {
                try {
                    viewHolder.anchorNameText.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            viewHolder.fansNum.setText(info.getViewCount());
            viewHolder.livegift.setText("1000");
            viewHolder.liveman.setText(info.getViewCount());
            String fengmian = info.getLiveCoverUrl();
            if (fengmian != null && fengmian.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_375(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(fengmian, size));
                if (cacheBitmap != null) {
                    viewHolder.fengmianImg.setImageBitmap(cacheBitmap);
                } else {
                    viewHolder.fengmianImg.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.fengmianImg, fengmian, size);
                }
            }
            String liveContent = info.getLiveContent();
            try {
                liveContent = URLDecoder.decode(liveContent, "utf-8");
                Log.e("info ", liveContent);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            viewHolder.liveDesc.setText(liveContent);

            String state = info.getLiveState();
            String atime = info.getAppointTime();
            if (state != null && state.length() > 0) {
                if (state.equals("0")) {
                    viewHolder.liveState.setBackgroundResource(R.drawable.state_jinxingzhong);
                    viewHolder.liveState.setText("直播中");
                } else if (state.equals("1")) {
                    viewHolder.liveState.setBackgroundResource(R.drawable.state_huifang);
                    viewHolder.liveState.setText(R.string.play_back);
                } else if (state.equals("5")) {
                    viewHolder.liveState.setBackgroundResource(R.drawable.state_yugao);
                    viewHolder.liveState.setText(atime + "小时后");
                }
            }

            if (info.getLiveTitle() != null) {
                try {
                    viewHolder.liveTitle.setText(URLDecoder.decode(info.getLiveTitle(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            viewHolder.zhiboTimeText.setText(info.getLiveTime());
//            final int currentPosition = position;
            final String vid = info.getVid();
            final String isPlayer = info.getIsMoive2();
            final String liveState = info.getLiveState();
            final int liveType = info.getLiveType();
            final String title = info.getLiveTitle();
            final String anchor = info.getNickName();
            final String headUrl = info.getUserLogo();
            final int isCharge = info.getIsCharge();
            final String fee = info.getFee();
            final String isuse = info.getIsuse();
            final EatShowUserInfo userInfo = EatShowApp.getInstance().getmUserInfo();
            int vip = 0;
            String userId = null;
            if (userInfo != null) {
                userId = userInfo.getUserId();
                vip = userInfo.isVip();
            }
            String anchorId = info.getUserId();
            boolean isSelf = false;
            if (anchorId != null && anchorId.equals(userId)) {
                isSelf = true;
            }
            final boolean finalIsSelf = isSelf;
            if (liveType == 1) {
                viewHolder.charge_or_pwdText.setImageResource(R.drawable.fufei);
            } else if (liveType == 2) {
                viewHolder.charge_or_pwdText.setImageResource(R.drawable.simi);
            } else if (liveType == 3) {
                viewHolder.charge_or_pwdText.setImageResource(R.drawable.vip);
            } else {
                viewHolder.charge_or_pwdText.setImageResource(0);
            }
            final int finalVip = vip;
            convertView.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   if (finalIsSelf || liveType == 0 || (liveType == 1 && isCharge == 1)) {
                                                       Intent intent = new Intent(mContext, PlayerNewActivity.class);
                                                       intent.putExtra("isUse",isuse);
                                                       intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                                                       intent.putExtra(Constants.LIVE_STATE, liveState);
                                                       intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                                                       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                       mContext.startActivity(intent);
                                                   } else if (liveType == 1 && isCharge == 0) {
                                                       //收费
                                                       Intent intent = new Intent(mContext, GouMaiKeChengActivity.class);
                                                       intent.putExtra("From", 1);
                                                       intent.putExtra("TITLE", title);
                                                       intent.putExtra("ZUOZHE", anchor);
                                                       intent.putExtra("FEE", fee);
                                                       intent.putExtra("isUse",isuse);
                                                       intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                                                       intent.putExtra(Constants.LIVE_STATE, liveState);
                                                       intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                                                       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                       mContext.startActivity(intent);
                                                   } else if (liveType == 2) {
                                                       //密码
                                                       Intent intent = new Intent(mContext, ZhiboPasswordActivity.class);
                                                       intent.putExtra("head_url", headUrl);
                                                       intent.putExtra("nickname", anchor);
                                                       intent.putExtra("isUse",isuse);
                                                       intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                                                       intent.putExtra(Constants.LIVE_STATE, liveState);
                                                       intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                                                       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                       mContext.startActivity(intent);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                                                   } else if (liveType == 3 && finalVip != 0) {
                                                       Intent intent = new Intent(mContext, PlayerNewActivity.class);
                                                       intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                                                       intent.putExtra(Constants.LIVE_STATE, liveState);
                                                       intent.putExtra("isUse",isuse);
                                                       intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                                                       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                       mContext.startActivity(intent);
                                                   } else if (liveType == 3 && finalVip == 0) {
                                                       Toast.makeText(mContext, "请购买VIP后观看", Toast.LENGTH_SHORT).show();
                                                   }
                                               }
                                           }

            );
        }
        return convertView;
    }


    class ViewHolder {
        ImageView anchorImgView;
        TextView livegift;
        TextView liveman;
        TextView fansNum;
        TextView anchorNameText;
        ImageView fengmianImg;
        TextView liveTitle;
        TextView liveState;
        ImageButton charge_or_pwdText;
        TextView zhiboTimeText;
        TextView liveDesc;
    }
}
